package com.chinaztt.fda.test.glide;

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.GlideModule;
import com.chinaztt.fda.utils.Log;

/**
 * @Description 自定义GlideModule，主要设置 自定义内存缓存和磁盘缓存
 * @Creator wushan
 * @Date 2016/10/11 14:41
 */
public class MyGlideModule implements GlideModule {

    public final static String DISKCACHE_PATH = "/GlideCache/";   // 磁盘缓存路径
    public final static int    DISKCACHE_SIZE = 30 * 1024 * 1024;// 磁盘缓存大小(当前30M)
    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        //获取系统分配给应用的总内存大小
        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        //设置图片内存缓存占用八分之一
        int memoryCacheSize = maxMemory / 8;
        //设置内存缓存大小
        Log.d("MyGlideModule","memoryCacheSize = " + memoryCacheSize);
        builder.setMemoryCache(new LruResourceCache(memoryCacheSize));

        //设置磁盘路径和缓存大小
        final String diskFullPath = context.getExternalCacheDir().getPath() + DISKCACHE_PATH;

        Log.d("MyGlideModule","diskFullPath = " + diskFullPath);
        builder.setDiskCache(
                new DiskLruCacheFactory(diskFullPath, "glide", DISKCACHE_SIZE)
        );
    }

    @Override
    public void registerComponents(Context context, Glide glide) {

    }
}
